package com.leicx.guli.product.app;

import com.leicx.guli.common.utils.PageUtils;
import com.leicx.guli.common.utils.R;
import com.leicx.guli.product.entity.AttrEntity;
import com.leicx.guli.product.entity.AttrGroupEntity;
import com.leicx.guli.product.service.AttrAttrgroupRelationService;
import com.leicx.guli.product.service.AttrGroupService;
import com.leicx.guli.product.service.CategoryService;
import com.leicx.guli.product.vo.attr.AttrGroupRelationVo;
import com.leicx.guli.product.vo.attr.AttrGroupWithAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author lcx
 * @email at_xiong@163.com
 * @date 2021-03-25 11:13:58
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable(name = "catId") Long catId) {
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long[] categoryPath = categoryService.getPathByCatId(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(categoryPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }


    /**
     * 获取attrGroup关联的属性
     */
    @GetMapping("/{attrGroupId}/attr/relation")
    public R attrRelationList(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrEntity> list = attrGroupService.attrRelationList(attrGroupId);
        return R.ok().put("data", list);
    }

    /**
     * 获取attrGroup关联的属性
     */
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R attrNoRelationList(@PathVariable("attrGroupId") Long attrGroupId,
                                @RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.attrNoRelationList(attrGroupId, params);
        return R.ok().put("page", page);
    }

    /**
     * 删除
     */
    @PostMapping("/attr/relation/delete")
    public R attrRelationDelete(@RequestBody AttrGroupRelationVo[] attrGroupRelations) {
        attrAttrgroupRelationService.attrRelationDelete(attrGroupRelations);
        return R.ok();
    }

    /**
     * 关联属性和分组
     */
    @PostMapping("/attr/relation")
    public R attrRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelations) {
        attrAttrgroupRelationService.attrRelation(attrGroupRelations);
        return R.ok();
    }

    /**
     * 获取三级分类下的属性分组和属性集合
     */
    @GetMapping("/{catLogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catLogId") Long catLogId) {
        List<AttrGroupWithAttrVo> list = attrGroupService.getAttrGroupWithAttrs(catLogId);
        return R.ok().put("data", list);
    }

}
